package com.iamback.dy.examine.controller.admin.examine;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamback.dy.entity.CommentExuser;
import com.iamback.dy.entity.CommentExuserView;
import com.iamback.dy.entity.DyContentComment;
import com.iamback.dy.entity.User;
import com.iamback.dy.examine.common.JsonResult;
import com.iamback.dy.examine.common.Page;
import com.iamback.dy.examine.controller.BaseController;
import com.iamback.dy.examine.controller.modle.Payload;
import com.iamback.dy.examine.service.ICommentExuserService;
import com.iamback.dy.examine.service.IDyContentCommentService;

@Controller
@RequestMapping("/examine/comment")
public class CommentExuserController extends BaseController{
   
	@Autowired
	public ICommentExuserService commentExuserService;
	@Autowired
	public IDyContentCommentService contentCommentService;
	
	
	@RequestMapping(value ={"/index"})
	public String  examineComment(){
		return "examine/comment";
	}
	
	@RequestMapping(value = { "/list" })
	@ResponseBody
	private Page<CommentExuserView> pageCommentExuser(Integer pageNumber,Integer pageSize){
		User userId =(User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
		if(userId!=null){ 
			Page<CommentExuserView> page  = commentExuserService.findCommentExuserPage(userId.getId(),pageNumber,pageSize);
			if(page.getTotalElements()<20){
				commentExuserService.distributionBy(userId.getId(),(int)page.getTotalElements());
			}
			return page;
			}
		return null;
	}
	
	
	@RequestMapping(value = "/update")
	@ResponseBody
	public JsonResult updateCommentExuser(@RequestBody List<Payload>  payloads){
		try{
			User userId =(User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
			if(userId==null){
				throw new Exception();
			}
		    String  uuid = UUID.randomUUID().toString();
		    if(!payloads.isEmpty()){
		    	for(Payload payload :payloads){
		    		CommentExuser commentExuser =commentExuserService.findCommentExuserById(payload.getId());
		    		Long status=0L;
		    		if(payload.getExResult().equals("1")){ 
		    			status=0L;
		    		}else if(payload.getExResult().equals("2")){
		    			status=3L;
		    		}
		    		if(commentExuser.getExResult().equals("0")&&commentExuser.getExuserId()==userId.getId()){
			    		commentExuser.setExResult(payload.getExResult());
		    			commentExuser.setExTime(new Date());
		    			commentExuser.setOperationCode(uuid);
		    			commentExuser.setExuserId(userId.getId());
		    			
		    			DyContentComment contentComment =new DyContentComment();
		    			contentComment.setCommentId(commentExuser.getCommentId());
		    			contentComment.setCommentType(status);
		    			//修改评论结果
		    			contentCommentService.updateContentComment(contentComment);
		    			//修改 审核记录
		    			commentExuserService.updateCommentExuser(commentExuser); 
		    		}
		    	}
		    }
			return JsonResult.success();
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		} 
	}
	
}
